﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using TestSystem.Model;
using TestSystem.Model.FrameworkEnumeration;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Auth.Attribute;
using WalkingTec.Mvvm.Mvc;

namespace TestSystem.Areas.API.Controllers
{
    [Area("API")]
    [AuthorizeJwtWithCookie]
    [ActionDescription("账号考试/练习")]
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class ExaminationSetupWithAcApiController : BaseApiController
    {


        /// <summary>
        /// 获取登录用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetUserInfo()
        {
            var user = await DC.Set<WebUser>()
                .Include(x => x.UnitWork)
                .Select(x => new
                {
                    ID = x.ID,
                    UserName = x.Name,
                    UnitWork = x.UnitWork.UnitWorkName,
                    PhotoId = x.PhotoId
                })
                .SingleOrDefaultAsync(x => x.ID == LoginUserInfo.Id);
            return Ok(user);

        }
        /// <summary>
        /// 获取所有状态正常考试信息
        /// </summary>
        /// <returns></returns>
        [ActionDescription("获取所有状态正常考试信息")]
        [HttpGet]
        public async Task<IActionResult> AllAtctivitiesList()
        {
            var list = await DC.Set<ExaminationSetup>()
                .Where(x => x.CourseEnum == CourseEnum.正常 && x.ParticipationTypes == ParticipationTypes.账号)
                .Where(x => x.StrTime <= DateTime.Now && DateTime.Now <= x.EndTime)
                .Select(x => new
                {
                    id = x.ID,
                    title = x.Title,
                    strTime = x.StrTime.ToString("yyyy-MM-dd"),
                    endTime = x.EndTime.ToString("yyyy-MM-dd"),
                    testTime = x.TestTime
                })
                .ToListAsync();
            return Ok(list);
        }
        /// <summary>
        /// 获取单场考试信息
        /// </summary>
        /// <param name="id">试卷id</param>
        /// <returns></returns>
        [ActionDescription("获取单场考试信息")]
        [HttpGet]
        public async Task<IActionResult> AtctivitiesListOne(int id)
        {
            //获取考试数据
            var es = await DC.Set<ExaminationSetup>()
                .SingleOrDefaultAsync(x => x.ID == id);
            //获取试卷配套轮播图
            var lisrrc = await DC.Set<RotationChart>()
                .Where(x => x.ExaminationSetupID == id)
                .ToListAsync();
            var redata = new
            {
                ks = es,
                lbt = lisrrc
            };
            return Ok(redata);
        }
        /// <summary>
        /// 试题收藏
        /// </summary>
        /// <param name="questionId">问题id</param>
        /// <returns></returns>
        [ActionDescription("试题收藏")]
        [HttpGet]
        public async Task<IActionResult> QuestionsCollection(int questionId)
        {
            if (questionId == 0)
            {
                return Content("系统错误");
            }
            Collection collection = new Collection
            {
                QuestionID = questionId,
                WebUserID = LoginUserInfo.Id
            };
            await DC.Set<Collection>().AddAsync(collection);
            int q = await DC.SaveChangesAsync();
            if (q > 0)
            {
                return Content("收藏成功");
            }
            return Content("系统错误");

        }
        [ActionDescription("参加考试")]
        [HttpGet]
        public async Task<IActionResult> PartExamination(int id)
        {
            var recordWithAccount = await DC.Set<RecordWithAccount>()
                .SingleOrDefaultAsync(x => x.ExaminationSetupID == id && x.WebUserID == LoginUserInfo.Id);
            if (recordWithAccount == null)
            {
                RecordWithAccount record = new RecordWithAccount
                {
                    ExaminationSetupID = id,
                    ParticipationTime = DateTime.Now,
                    WebUserID = LoginUserInfo.Id,
                    Achievement = 0
                };
                await DC.Set<RecordWithAccount>().AddAsync(record);
                int q = await DC.SaveChangesAsync();
                if (q > 0)
                {
                    return Content(record.ID.ToString());
                }
                return Content("系统错误");
            }
            else
            {
                return Content("您已参加过本场考试，感谢您的参与");
            }

            //检查是否参加考试

        }


    }
}
